package com.caiheng.api.commons.config;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.DynamicTableNameInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.caiheng.api.config.DynamicTableTreadLocal;
import com.caiheng.api.config.RequestDataHelper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

/**
 * @Package: com.huanchuang.common.config
 * @Description: <mybatis-plush配置类>
 * @Author: MILLA
 * @CreateDate: 2020/09/04 14:42
 * @UpdateUser: MILLA
 * @UpdateDate: 2020/09/04 14:42
 * @UpdateRemark: <>
 * @Version: 1.0
 */
@Configuration
@MapperScan("com.caiheng.api.dao")
public class MybatisPlusDynamicTableConfig {

    static List<String> tableList(){
        List<String> tables = new ArrayList<>();
//        tables.add("charger_port_record");
        return tables;
    }

    /**
     * 分页插件, 对于单一数据库类型来说,都建议配置该值,避免每次分页都去抓取数据库类型
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
//        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
//        return interceptor;

        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
        dynamicTableNameInnerInterceptor.setTableNameHandler((sql, tableName) -> {
            // 获取参数方法
//            Map<String, Object> paramMap = RequestDataHelper.getRequestData();
//            paramMap.forEach((k, v) -> System.err.println(k + "----" + v));
//
//            System.err.println("表名:"+tableName);
//            System.err.println("sql:"+sql);
//            String year = "_2018";
//            int random = new Random().nextInt(10);
//            if (random % 2 == 1) {
//                year = "_2019";
//            }
//            return tableName + year;
            String newTable = null;
            for (String table : tableList()) {
                newTable = RequestDataHelper.getRequestData(table);
//                System.out.println("newTable: " + newTable);
                if (newTable!=null && table.equals(tableName)){
                    tableName = newTable;
//                    System.out.println("tableName: " + tableName);
                    break;
                }
            }
            return tableName;
        });
        interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        // 3.4.3.2 作废该方式
        // dynamicTableNameInnerInterceptor.setTableNameHandlerMap(map);
        return interceptor;
    }

    @Bean
    public DynamicTableNameInnerInterceptor getDynamicTableNameInterceptor() {
        DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
        dynamicTableNameInnerInterceptor.setTableNameHandler((sql, tableName) -> {
            String tableName1 = DynamicTableTreadLocal.INSTANCE.getTableName();
            return StrUtil.isNotEmpty(tableName1) ? tableName1 : tableName;
        });
        return dynamicTableNameInnerInterceptor;
    }

}
